#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
  int eraseOverlapIntervals(vector<vector<int>> &intervals) {
    if (intervals.empty() || intervals.size() == 1) {
      return 0;
    }

    sort(intervals.begin(), intervals.end(),
         [](const auto &u, const auto &v) { return u[0] < v[0]; });
    int res = 0;
    for (int i = 0, j = 1; j <= intervals.size();) {
      if (j == intervals.size()) {
        ++res;
        break;
      }
      if (intervals[i][1] <= intervals[j][0]) {
        i = j;
        ++j;
	++res;
      } else {
        if (intervals[i][1] <= intervals[j][1]) {
          ++j;
        } else {
          i = j;
          j = i + 1;
        }
      }
    }
    if (res == 0) {
      return intervals.size() - 1;
    }

    return intervals.size() - res;
  }
};

int main() {
  vector<vector<int>> a = {{81, 97}, {-71, 60}, {36, 97}, {76, 96}};
  cout << Solution().eraseOverlapIntervals(a) << endl;
}
